package com.hdoit.ares.logical.advertisement.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.advertisement.dao.IAdvertisementLocationDao;
import com.hdoit.ares.logical.advertisement.model.AdvertisementLocation;
import com.hdoit.ares.logical.viewmodel.advertisement.AdvertisementLocationSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年03月13日 19:02:37
 * @comment
 */
@Component
public class AdvertisementLocationDaoImpl extends SuperDao<AdvertisementLocation> implements IAdvertisementLocationDao{

	@Override
	public List<AdvertisementLocation> queryListBySearch(AdvertisementLocationSearch search,
			PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
		                .from(AdvertisementLocation.class)
		                .addOrder("createTime", OrderEnum.desc);
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	private void appendFinder(AdvertisementLocationSearch search, IFinderExpression finderExpression) {
		if(!StringUtils.isEmpty(search.getName())){
			finderExpression.andWhere("name like #{name}")
				  			.setParam("name", "%" + search.getName() + "%");
		}
		if(StringUtils.isEmpty(search.getIsOpen())){
			finderExpression.andWhere("isOpen like #{isOpen}")
				  			.setParam("isOpen", search.getIsOpen());
		}
		
		if(search.getStartTime()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},createTime)>=0")
				  			.setParam("startDate", search.getStartTime());
		}
		if(search.getEndTime()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},createTime)<=0")
				  			.setParam("endDate", search.getEndTime());
		}
		
	}

	@Override
	public List<AdvertisementLocation> queryList(AdvertisementLocationSearch search) {
		return queryListBySearch(search, null);
	}

	@Override
	public int queryCount(AdvertisementLocationSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("count(*)")
		                .from(AdvertisementLocation.class);
		
		appendFinder(search,finderExpression);
		return queryCount(finderExpression);
	}
	
	

}